﻿using SL651Center2.x.Enums;
using System.Text;
using TouchSocket.Core;

namespace SL651Center2.x.Messages
{
    public class MessageImg : IMessage
    {
        /// <summary>
        /// 流水号
        /// </summary>
        public byte[] SerialNumber { get; set; } = Array.Empty<byte>();
        /// <summary>
        /// 流水号数字形式
        /// </summary>
        public ushort SerialNum { get; set; }
        /// <summary>
        /// 发报时间
        /// </summary>
        public byte[] SendTime { get; set; } = Array.Empty<byte>();
        /// <summary>
        /// 发报时间字符串形式
        /// </summary>
        public string SendTimeStr { get; set; } = string.Empty;
        /// <summary>
        /// 遥测站地址
        /// </summary>
        public byte[] TelemetryAddress { get; set; } = Array.Empty<byte>();
        /// <summary>
        /// 遥测站地址的字符串形式
        /// </summary>
        public string TelemetryAddressStr { get; set; } = string.Empty;
        /// <summary>
        /// 遥测站分类码
        /// </summary>
        public TelemetryStationCode TelemetryStation { get; set; }
        /// <summary>
        /// 观测时间
        /// </summary>
        public byte[] ObservationTime { get; set; } = Array.Empty<byte>();
        /// <summary>
        /// 观测时间字符串
        /// </summary>
        public string ObservationTimeStr { get; set; } = string.Empty;

        /// <summary>
        /// 图片存储位置
        /// </summary>
        public string ImgPath { get; set; } = string.Empty;

        public void Read(in ByteBlock byteBlock)
        {
            //流水号
            SerialNumber = byteBlock.ReadToSpan(2).ToArray();
            SerialNum = Convert.ToUInt16(Helper.ToHEXString(SerialNumber), 16);

            //发报时间
            SendTime = byteBlock.ReadToSpan(6).ToArray();
            SendTimeStr = Helper.ByteToBCDTime(SendTime);

            //遥测站地址
            var code = byteBlock.ReadByte();
            byteBlock.ReadByte();
            TelemetryAddress = byteBlock.ReadToSpan(5).ToArray();
            TelemetryAddressStr = Helper.ToHEXString(TelemetryAddress);

            //遥测站分类码
            TelemetryStation = (TelemetryStationCode)byteBlock.ReadByte();

            //观测时间
            _ = byteBlock.ReadByte();
            byteBlock.Seek(1, SeekOrigin.Current);
            ObservationTime = byteBlock.ReadToSpan(5).ToArray();
            ObservationTimeStr = Helper.ByteToBCDTime(ObservationTime);

            var elemCode = byteBlock.ReadByte();
            if (elemCode != 0x03 && elemCode != 0x17)
            {
                //跳过数据定义
                _ = byteBlock.ReadByte();
                //图片信息
                if ((ElementCode)elemCode == ElementCode.PIC)
                {
                    var img = byteBlock.ToArray(byteBlock.Position);
                    ImgPath = Helper.ByteToImg(img);
                }
            }
        }

        public void ReadASCII(in ByteBlock byteBlock)
        {
            //流水号
            SerialNumber = byteBlock.ReadToSpan(4).ToArray();
            SerialNum = Convert.ToUInt16(SerialNumber.ToASCIIString(), 16);

            //发报时间
            SendTime = byteBlock.ReadToSpan(12).ToArray();
            SendTimeStr = Helper.StringToBCDTime(SendTime.ToASCIIString());

            //获取遥测站地址到观测时间后空格
            var datab = new byte[30];
            byteBlock.Read(datab);
            var datastr = datab.ToASCIIString();
            var arr = datastr.Split(" ");

            //遥测站地址
            TelemetryAddressStr = arr[1];
            TelemetryAddress = Encoding.ASCII.GetBytes(TelemetryAddressStr);

            //遥测站分类码
            var typestr = arr[2];
            if (Enum.IsDefined(typeof(TelemetryStationCode), typestr))
            {
                var typecode = (TelemetryStationCode)Enum.Parse(typeof(TelemetryStationCode), typestr);
                TelemetryStation = typecode;
            }

            //观测时间
            var tt = arr[4];
            ObservationTime = Encoding.ASCII.GetBytes(tt);
            ObservationTimeStr = Helper.StringToBCDTime(tt);

            //获取标识符
            var elem = new byte[3];
            byteBlock.Read(elem);
            var elemstr = elem.ToASCIIString();
            if (Enum.IsDefined(typeof(ElementCode), elemstr))
            {
                var elemCode = (ElementCode)Enum.Parse(typeof(ElementCode), elemstr);

                //跳过空格
                byteBlock.Seek(1, SeekOrigin.Current);

                //图片信息
                if (elemCode == ElementCode.PIC)
                {
                    var img = byteBlock.ToArray(byteBlock.Position);
                    ImgPath = Helper.ByteToImg(img.ToArray(), "ASCII.jpg");
                    Console.WriteLine(ImgPath);
                }

            }

        }

    }
}
